package com.yexiong.server.process;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.swing.JOptionPane;

import com.common.entity.CheckDetail;
import com.common.entity.CheckRecord;
import com.common.entity.DataPack;
import com.common.server.view.ServerMainFrame;
import com.common.server.view.ServerProcessThread;
import com.common.server.view.ServiceProcess;
import com.yexiong.dao.ICheckDetailDao;
import com.yexiong.dao.impl.CheckDetailDaoImpl;
import com.yexiong.dao.impl.CheckRecordDaoImpl;
import com.yexiong.server.dao.ICheckDetailServer;
import com.yexiong.server.dao.ICheckServer;
import com.yexiong.server.dao.impl.CheckDetailServerImpl;
import com.yexiong.server.dao.impl.CheckServerImpl;

public class CheckDetailServiceProcess implements ServiceProcess{
	private ICheckServer ICheckServer=new CheckServerImpl();
	private ICheckDetailServer ICheckDetailServer=new CheckDetailServerImpl();
	@Override
	public Object service(DataPack dataPack, ServerProcessThread serverProcessThread, ServerMainFrame serverMainFrame)
			throws IOException {
		if (dataPack.getOptType().equals("delete")) {
			CheckRecord checkRecord=(CheckRecord)dataPack.getCon();
			String date=new SimpleDateFormat("yyyy-MM-dd").format(checkRecord.getCheckDate());
			int checkId=ICheckServer.getId(date, checkRecord.getCheckStuId());
			int id=ICheckDetailServer.delete(new SimpleDateFormat("yyyy-MM-dd").format(checkRecord.getCheckStartTime()),checkId);
			List<Integer> list=new ArrayList<>();
			list.add(checkId);
			list.add(id);
			serverProcessThread.getOos().writeObject(list);
			serverProcessThread.getOos().flush();
		}else if (dataPack.getOptType().equals("getId")) {
			CheckDetail checkDetail=(CheckDetail)dataPack.getCon();
			int id=ICheckDetailServer.getId(checkDetail.getDetailCheckId(),new SimpleDateFormat("yyyy-MM-dd").format(checkDetail.getCheckDetailStartTime()) );
			checkDetail.setCheckDetailId(id);
			serverProcessThread.getOos().writeObject(checkDetail);
			serverProcessThread.getOos().flush();
		}else if (dataPack.getOptType().equals("update")) {
			CheckDetail checkDetail=(CheckDetail)dataPack.getCon();
			String startDate=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(checkDetail.getCheckDetailStartTime());
			String endDate=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(checkDetail.getCheckDetailEndTime());
			startDate=(startDate.split(" "))[1].replace(":", "");
			endDate=(endDate.split(" "))[1].replace(":", "");
			boolean isTrue=ICheckDetailServer.isTrueTime(Integer.parseInt(startDate), Integer.parseInt(endDate), checkDetail.getDetailCheckId());
			int result=0;
			DataPack dataPack2=new DataPack();
			if (isTrue) {
				result=ICheckDetailServer.update(checkDetail);
				dataPack2.setCon(result);
				serverProcessThread.getOos().writeObject(dataPack2);
				serverProcessThread.getOos().flush();
			}else {
				dataPack2.setCon(result);
				serverProcessThread.getOos().writeObject(dataPack2);
				serverProcessThread.getOos().flush();
			}
		}else if (dataPack.getOptType().equals("add")) {
			CheckDetail checkDetail=(CheckDetail)dataPack.getCon();
			int result=0;
			result=ICheckDetailServer.insert(checkDetail);
			serverProcessThread.getOos().writeObject(result);
			serverProcessThread.getOos().flush();
		}else if (dataPack.getOptType().equals("sort")) {
			Set<Integer> idSet=(Set<Integer>)dataPack.getCon();
			Iterator<Integer> iterator=idSet.iterator();
			while (iterator.hasNext()) {
				int checkId=iterator.next();
				Date firstTime=ICheckDetailServer.getFirstStartDate(checkId);
				Date lastTime=ICheckDetailServer.getLastEndDate(checkId);
				ICheckServer.updateDate(firstTime, lastTime, checkId);
				
			}
		}else if (dataPack.getOptType().equals("show")) {
			int id=(int)dataPack.getCon();
			ICheckDetailDao iCheckDetailDao=new CheckDetailDaoImpl();
			List<CheckDetail> list=iCheckDetailDao.selectTime(id);
			DataPack dataPack2=new DataPack();
			dataPack2.setCon(list);
			serverProcessThread.getOos().writeObject(dataPack2);
			serverProcessThread.getOos().flush();
		}
		return null;
	}

}
